package com.frogs.ucenter.v1.service.impl;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.frogs.framework.util.PageList;
import com.frogs.system.web.api.request.Request;
import com.frogs.system.web.api.response.Response;
import com.frogs.system.web.exception.ServiceOperationException;
import com.frogs.ucenter.dao.UserMedicationInfoDao;
import com.frogs.ucenter.dao.UserMedicationInfoTemporaryDao;
import com.frogs.ucenter.entity.UserMedicationInfo;
import com.frogs.ucenter.entity.UserMedicationInfoTemporary;
import com.frogs.ucenter.v1.request.UserMedicationAddRequest;
import com.frogs.ucenter.v1.request.UserMedicationDeleteRequest;
import com.frogs.ucenter.v1.request.UserMedicationFollowedAddRequest;
import com.frogs.ucenter.v1.request.UserMedicationFollowedDeleteRequest;
import com.frogs.ucenter.v1.request.UserMedicationListRequest;
import com.frogs.ucenter.v1.response.AddResponse;
import com.frogs.ucenter.v1.response.DeleteResponse;
import com.frogs.ucenter.v1.response.PageListResponse;
import com.frogs.ucenter.v1.service.UserMedicationService;
import com.frogs.ucenter.v1.util.ApiConstants;

@Transactional(propagation=Propagation.REQUIRED, rollbackFor=Exception.class) 
@Service(ApiConstants.VERSION+"userMedicationService")
public class UserMedicationServiceImpl implements UserMedicationService {
	
	private static final Logger log = LoggerFactory.getLogger(UserMedicationServiceImpl.class);

	@Resource
	private UserMedicationInfoDao userMedicationInfoDao;
	
	@Resource
	private UserMedicationInfoTemporaryDao userMedicationInfoTemporaryDao;
	
	@Override
	public Response getMedicationInfoList(Request request) throws Exception {
		PageListResponse<UserMedicationInfo> response = new PageListResponse<UserMedicationInfo>();
		try {
			UserMedicationListRequest detailRequest = (UserMedicationListRequest) request;
			Map<String, Object> params = new HashMap<String, Object>();
			params.put("eqUserId", detailRequest.getUserId());
			PageList<UserMedicationInfo> userMedicationInfo = userMedicationInfoDao.selectByMap(params, false);
			response.setList(userMedicationInfo);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	    return response;
	}
	
	@Override
	public Response add(Request request) throws Exception {
		AddResponse<UserMedicationInfo> response = new AddResponse<UserMedicationInfo>();
		try {
			UserMedicationAddRequest addRequest = (UserMedicationAddRequest) request;
			UserMedicationInfo userMedicationInfo = new UserMedicationInfo();
			userMedicationInfo.setDosage(addRequest.getDosage());
			userMedicationInfo.setDrugName(addRequest.getDrugName());
			userMedicationInfo.setSymptoms(addRequest.getSymptoms());
			userMedicationInfo.setUseCondition(addRequest.getUseCondition());
			userMedicationInfo.setUserId(addRequest.getUserId());
			userMedicationInfoDao.insert(userMedicationInfo);
			response.setInfo(userMedicationInfo);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	    return response;
	}
	
	@Override
	public Response delete(Request request) throws Exception {
		DeleteResponse response = new DeleteResponse();
		try {
			UserMedicationDeleteRequest deleteRequest = (UserMedicationDeleteRequest) request;
			userMedicationInfoDao.deleteById(deleteRequest.getId());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	    return response;
	}
	
	@Override
	public Response followedAdd(Request request) throws Exception {
		AddResponse<UserMedicationInfoTemporary> response = new AddResponse<UserMedicationInfoTemporary>();
		try {
			UserMedicationFollowedAddRequest addRequest = (UserMedicationFollowedAddRequest) request;
			UserMedicationInfoTemporary userMedicationInfo = new UserMedicationInfoTemporary();
			userMedicationInfo.setDosage(addRequest.getDosage());
			userMedicationInfo.setDrugName(addRequest.getDrugName());
			userMedicationInfo.setSymptoms(addRequest.getSymptoms());
			userMedicationInfo.setUseCondition(addRequest.getUseCondition());
			userMedicationInfo.setUserId(addRequest.getUserId());
			userMedicationInfoTemporaryDao.insert(userMedicationInfo);
			response.setInfo(userMedicationInfo);
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	    return response;
	}
	
	@Override
	public Response followedDelete(Request request) throws Exception {
		DeleteResponse response = new DeleteResponse();
		try {
			UserMedicationFollowedDeleteRequest deleteRequest = (UserMedicationFollowedDeleteRequest) request;
			userMedicationInfoTemporaryDao.deleteById(deleteRequest.getId());
		} catch (Exception e) {
			log.error("func[{}] request[{}] exception[{}] desc[fail]",
					new Object[] { this.getClass().getName() +"."+Thread.currentThread().getStackTrace()[1].getMethodName(),
						request,Arrays.deepToString(e.getStackTrace()) });
			throw new ServiceOperationException("请求处理失败：" + e.getMessage());
		}
	    return response;
	}
}
